package com.example.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Roles;
import com.example.springboot.service.IRolesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/roles")
public class RolesController {

    @Resource
    private IRolesService rolesService;

    @GetMapping
    public Result findAll() {
        return Result.success(rolesService.list());
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//
//        if (!"".equals(username)) {
//            queryWrapper.like("username", username);
//        }
//        if (!"".equals(email)) {
//            queryWrapper.like("email", email);
//        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }
//        //获取当前用户信息
//        User currentUser= TokenUtils.getCurrentUser();
//        queryWrapper.orderByDesc("id");
        return Result.success( rolesService.findPage(new Page<>(pageNum, pageSize), name));
    }

    //新增或者更新
    @PostMapping
    public Result save(@RequestBody Roles roles) {
        return Result.success(rolesService.saveOrUpdate(roles));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(rolesService.removeById(id));
    }
}
